import {
    createRouter,
    createWebHashHistory
} from "vue-router";
import login from '../views/login/index.vue'
import home from '../views/home/index.vue'

const routes = [
    {
        path: "/",
        name: "Home",
        meta: {
            title: '主页'
        },
        component: home
    },
    {
        path: "/wxLogin",
        name: "wxLogin",
        meta: {
            title: '微信登录'
        },
        component: ()=> import("../views/wxLogin/index.vue")
    },
    {
        path: "/wx",
        name: "wx",
        meta: {
            title: 'wx聊天'
        },
        component: ()=> import("../views/index.vue"),
        children:[
            {
                path:'/wx/chat',
                name:'chat',
                meta:{
                    title:'会话列表'
                },
                component:()=>import("@/views/chat/index.vue")
            },
            {
                path:'/wx/user',
                name:'user',
                meta:{
                    title:'好友列表'
                },
                component:()=>import("@/views/userList/index.vue")
            },
        ]
    },
    {
    path: '/login',
    name: 'Login',
    meta: {
        title: '登录'
    },
    component: login
    },
    {
    path: '/userLogin',
    name: 'userLogin',
    meta: {
        title: '用户名密码登录'
    },
    component:()=>import("@/views/userLogin/index.vue")
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title}`;
    let token = localStorage.getItem("wx_token");
    if (token==null){
        if (to.path==='/login' || to.path==='/wxLogin' || to.path==='/' || to.path==='/userLogin'){
            next()
        }else {
            next('/login')
        }
    }else {
        if (to.path==='/login' || to.path==='/userLogin'){
            next('/wx/chat')
        }else {
            next()
        }
    }
})

export default router;

